<template>
	<view class="favorites-container">
		
		<!-- 标签页导航 -->
		<view class="tab-nav">
			<view class="tab-item active">
				<text class="tab-text">商品</text>
				<view class="tab-line"></view>
			</view>
		</view>
		
		<!-- 商品列表 -->
		<scroll-view 
			class="product-scroll" 
			scroll-y="true"
			refresher-enabled="true"
			:refresher-triggered="refreshing"
			@refresherrefresh="onRefresh"
			@refresherrestore="onRestore"
			@refresherabort="onAbort"
		>
			<view class="product-list">
				<!-- 加载状态 -->
				<view v-if="loading" class="loading-container">
					<text class="loading-text">加载中...</text>
				</view>
				
				<!-- 空状态 -->
				<view v-else-if="productList.length === 0" class="empty-container">
					<text class="empty-text">暂无收藏商品</text>
				</view>
				
				<!-- 商品列表 -->
				<view v-else class="product-item" v-for="(item, index) in productList" :key="index">
					<view class="product-image">
						<image :src="item.image || '/static/default-product.png'" mode="aspectFill"></image>
					</view>
					<view class="product-info">
						<view class="product-title">{{ item.title || item.skuName || '商品名称' }}</view>
						<view class="product-desc">{{ item.description || item.skuDesc || '商品描述' }}</view>
						<view class="product-price">¥{{ item.price || item.skuPrice || '0' }}</view>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import { get } from '@/utils/request.js';
	
	export default {
		data() {
			return {
				refreshing: false,
				loading: false,
				productList: [],
				membersId: null // 用户ID
			}
		},
		onLoad(options) {
			// 获取用户ID，可以从本地存储或页面参数中获取
			this.membersId = uni.getStorageSync('membersId') || options.membersId;
			if (this.membersId) {
				this.getFavoritesList();
			} else {
				uni.showToast({
					title: '用户信息获取失败',
					icon: 'none'
				});
			}
		},
		methods: {
			// 获取用户收藏商品列表
			async getFavoritesList() {
				if (!this.membersId) {
					uni.showToast({
						title: '用户ID不能为空',
						icon: 'none'
					});
					return;
				}
				
				this.loading = true;
				try {
					const response = await get(`/sku/${this.membersId}`);
					
					if (response.code === 200) {
						this.productList = response.data || [];
						console.log('收藏商品列表:', this.productList);
					} else {
						uni.showToast({
							title: response.msg || '获取收藏商品失败',
							icon: 'none'
						});
					}
				} catch (error) {
					console.error('获取收藏商品失败:', error);
					uni.showToast({
						title: '网络请求失败',
						icon: 'none'
					});
				} finally {
					this.loading = false;
				}
			},
			
			// 下拉刷新触发
			async onRefresh() {
				console.log('开始下拉刷新');
				this.refreshing = true;
				
				try {
					await this.getFavoritesList();
					uni.showToast({
						title: '刷新成功',
						icon: 'success',
						duration: 1500
					});
				} catch (error) {
					uni.showToast({
						title: '刷新失败',
						icon: 'none'
					});
				} finally {
					this.refreshing = false;
				}
			},
			
			// 刷新恢复
			onRestore() {
				console.log('刷新恢复');
			},
			
			// 刷新中止
			onAbort() {
				console.log('刷新中止');
			}
		}
	}
</script>

<style scoped>
	.favorites-container {
		background-color: #f5f5f5;
		min-height: 100vh;
		display: flex;
		flex-direction: column;
	}
	
	/* 标签页样式 */
	.tab-nav {
		display: flex;
		background-color: #fff;
		border-bottom: 1rpx solid #eee;
		flex-shrink: 0;
	}
	
	.tab-item {
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 20rpx 0;
		position: relative;
	}
	
	.tab-text {
		font-size: 32rpx;
		color: #666;
	}
	
	.tab-item.active .tab-text {
		color: #ff4444;
		font-weight: bold;
	}
	
	.tab-line {
		position: absolute;
		bottom: 0;
		width: 60rpx;
		height: 4rpx;
		background-color: #ff4444;
		border-radius: 2rpx;
	}
	
	/* 滚动区域样式 */
	.product-scroll {
		flex: 1;
		height: 0;
	}
	
	/* 商品列表样式 */
	.product-list {
		padding: 20rpx;
	}
	
	/* 加载状态样式 */
	.loading-container {
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 100rpx 0;
	}
	
	.loading-text {
		font-size: 28rpx;
		color: #999;
	}
	
	/* 空状态样式 */
	.empty-container {
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 200rpx 0;
	}
	
	.empty-text {
		font-size: 28rpx;
		color: #999;
	}
	
	.product-item {
		display: flex;
		background-color: #fff;
		margin-bottom: 20rpx;
		padding: 20rpx;
		border-radius: 12rpx;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
	}
	
	.product-image {
		width: 160rpx;
		height: 160rpx;
		margin-right: 20rpx;
		border-radius: 8rpx;
		overflow: hidden;
	}
	
	.product-image image {
		width: 100%;
		height: 100%;
	}
	
	.product-info {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}
	
	.product-title {
		font-size: 28rpx;
		color: #333;
		font-weight: bold;
		line-height: 1.4;
		margin-bottom: 10rpx;
	}
	
	.product-desc {
		font-size: 24rpx;
		color: #999;
		line-height: 1.3;
		margin-bottom: 10rpx;
	}
	
	.product-price {
		font-size: 32rpx;
		color: #ff4444;
		font-weight: bold;
	}
</style>
